package com.jinmei.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.map.MultiValueMap;
import org.springframework.dao.DataAccessException;

import com.jinmei.dao.WorkScheduleDao;
import com.jinmei.model.WorkSchedule;
import com.jinmei.pushclient.PushClientProxy;
import com.jinmei.service.WorkScheduleService;
import com.jinmei.web.ws.NotificationMessageCreator;

public class WorkScheduleServiceImpl implements WorkScheduleService {

	private WorkScheduleDao workScheduleDao;
	private PushClientProxy pushClient;
	
	public WorkScheduleDao getWorkScheduleDao() {
		return workScheduleDao;
	}

	public void setWorkScheduleDao(WorkScheduleDao workScheduleDao) {
		this.workScheduleDao = workScheduleDao;
	}
	
	public PushClientProxy getPushClient() {
		return pushClient;
	}

	public void setPushClient(PushClientProxy pushClient) {
		this.pushClient = pushClient;
	}
	
	public List<WorkSchedule> getWorkSchedulesByDate(String startDate,
			String endDate) throws DataAccessException, ParseException {
		
		List<WorkSchedule> workSchedules = workScheduleDao
				.findWorkSchedulesByDate(startDate, endDate);

		return workSchedules;
	}

	@Override
	public List<WorkSchedule> getWorkSchedulesByDateAndType(String workingDate,
			String workingType) throws DataAccessException, ParseException {
		
		List<WorkSchedule> workSchedules = workScheduleDao
				.findWorkSchedulesByDateAndType(workingDate, workingType);
		return workSchedules;
	}
	
	@Override
	public List<WorkSchedule> getWorkSchedulesByDate(Date workDate)
			throws DataAccessException, ParseException {
		return getWorkScheduleDao().findWorkSchedulesByDate(workDate);
	}

	@Override
	public void saveOrUpdate(List<WorkSchedule> entities) {
		getWorkScheduleDao().saveOrUpdate(entities);
	}
	
	@Override
	public void delete(List<WorkSchedule> entities) {
		getWorkScheduleDao().delete(entities);
	}

	@Override
	public void updateWorkSchedules(List<WorkSchedule> oldWSByDate,
			List<WorkSchedule> newWSByDate) {
		getWorkScheduleDao().delete(oldWSByDate);
		getWorkScheduleDao().saveOrUpdate(newWSByDate);	
	}

	@Override
	public void sendNotification(Date workDate,
			MultiValueMap oldMultiWSMapByEmployeeId,
			MultiValueMap newMultiWSMapByEmployeeId) throws DataAccessException {
		NotificationMessageCreator msgCreator = 
				new NotificationMessageCreator(oldMultiWSMapByEmployeeId, newMultiWSMapByEmployeeId, workDate);
		getPushClient().sendMessagesToMany(msgCreator.create());
		
	}

}
